package com.niu.wm.web.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.niu.wm.core.ResponseObject;
import com.niu.wm.core.error.BizException;
import com.niu.wm.core.error.GlobalErrorCode;
import com.niu.wm.model.User;
import com.niu.wm.service.UserService;

@Controller
@RequestMapping(value = "/user")
public class UserController extends BaseController{
	@Autowired
	private UserService userService;
	
	
	@RequestMapping(value = "/changePwd.do")
	public String changePwd(ModelMap model){
		User user = this.getCurrentUser();
		model.addAttribute("user", user);
		
		return "user/changePwd";
	}
	
	@RequestMapping(value = "/doChangePwd.do")
    @ResponseBody
	public ResponseObject<Boolean> doChangePwd(String account, String oldPasswd, String newPasswd){
		User user = this.getCurrentUser();
		User u = userService.loadByAccount(account);
				
		Boolean canChange = Boolean.TRUE;
		if( u == null){
			canChange = Boolean.FALSE;
		}
		else if( !user.getId().equals(u.getId()) ){
			//修改他人密码
			if( !user.getAccount().equalsIgnoreCase("system")){
				canChange = Boolean.FALSE;
			}
		}
	
		if(canChange){
			userService.changePwd(u.getId(), newPasswd);
			return new ResponseObject<Boolean>(Boolean.TRUE);
		}else{
			return new ResponseObject<Boolean>(Boolean.FALSE);
		}
	}
	
	@RequestMapping(value = "/test.do")
    @ResponseBody
	public ResponseObject<String> test(String account, String oldPasswd, String newPasswd){
		return new ResponseObject<String>("Hello world");
	}
}
